import {getToken, removeToken} from '../../../utils/token';
import {UserAction, PostsActionType, PostsState} from './interface';

const getDefaultUserState = (): PostsState => ({
  id: 0,
  thumb_url: '',
  content: '',
  type: 0,
  create_time: new Date(),
  update_time: new Date(),
  title: '',
  author: ''
});

export const userReducer = (
  state = getDefaultUserState(),
  action: UserAction
) => {
  switch (action.type) {
    case PostsActionType.RESET_STATE: {
      removeToken();
      return getDefaultUserState();
    }
    case PostsActionType.SET_ID:
      return {...state, ...action.payload};
    case PostsActionType.SET_THUMB_URL:
      return {...state, ...action.payload};
    case PostsActionType.SET_CONTENT:
      return {...state, ...action.payload};
    case PostsActionType.SET_TYPE:
      return {...state, ...action.payload};
    case PostsActionType.SET_CREATE_TIME:
      return {...state, ...action.payload};
    case PostsActionType.SET_UPDATE_TIMR:
      return {...state, ...action.payload};
    case PostsActionType.SET_TITLE:
      return {...state, ...action.payload};
    default: {
      const _: never = action;
      return state;
    }
  }
};
